package engine.logic.walk
{
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class FindWayUtil
	{
		private var _startRolePoint	: Point;
		private var _RoleWidth			: int;
		private var _RoleHiehgt 		: int
		
		private var _startNPCPoint	: Point;
		private var _NPCWidth			: int;
		private var _NPCHiehgt 		: int
		
		public var RoleWalkRect		:Rectangle;
		
		/**
		 *	更新角色行走区域 
		 */		
		public function updaWalkPlace( points : Array ):void
		{
			_startRolePoint = new Point( points[ 0 ], points[ 1 ] );
			_RoleWidth 		= points[ 2 ];
			_RoleHiehgt		= points[ 3 ] - 10;
			RoleWalkRect	=	new Rectangle(_startRolePoint.x,_startRolePoint.y,_RoleWidth,_RoleHiehgt);
		}
		
		/**
		 *	更新NPC行走区域 
		 */		
		public function updaeNPCWalkPlace( points : Array ):void
		{
			_startNPCPoint = new Point( points[ 0 ], points[ 1 ] );
			_NPCWidth 		= points[ 2 ];
			_NPCHiehgt		= points[ 3 ];
		}
		
		/**
		 *	能否行走
		 * @param walkPlace 	行走区域
		 * @param clickPoint	点击的坐标
		 * @return 
		 * 
		 */
		public function isWalk(clickPoint : Point):Boolean
		{
			//先判断点击的点是否属于行走区域
			if(clickPoint.y < _startRolePoint.y || clickPoint.y > _startRolePoint.y + _RoleHiehgt )
			{
				return false;	
			}
			if(clickPoint.x < _startRolePoint.x || clickPoint.x > _startRolePoint.x + _RoleWidth )
			{
				return false;	
			}
			
			return true;
		}
		
		public function isNPCWalk(clickPoint : Point):Boolean
		{
			//先判断点击的点是否属于行走区域
			if(clickPoint.y < _startNPCPoint.y || clickPoint.y > _startNPCPoint.y + _NPCHiehgt )
			{
				return false;	
			}
			if(clickPoint.x < _startNPCPoint.x || clickPoint.x > _startNPCPoint.x + _NPCWidth )
			{
				return false;	
			}
			
			return true;
		}
		
		/**
		 *	获取Role行走坐标，当点击不可行走区域的时候
		 * @return point
		 * 
		 */		
		public function getWalkNode(clickPoint : Point):Point
		{
			if(isWalk(clickPoint))
			{
				return clickPoint;
			}
			
			if( clickPoint.y < _startRolePoint.y )
			{
				clickPoint.y = _startRolePoint.y;
			}else if( clickPoint.y > _startRolePoint.y + _RoleHiehgt )
			{
				clickPoint.y = _startRolePoint.y + _RoleHiehgt;
			}
			
			if(clickPoint.x < _startRolePoint.x)
			{
				clickPoint.x = _startRolePoint.x;
			}
			else if(clickPoint.x > _startRolePoint.x + _RoleWidth)
			{
				clickPoint.x = _startRolePoint.x + _RoleWidth;
			}
			
			return clickPoint;
		}
		
		/**
		 *	获取NPC行走坐标
		 * @return point
		 * 
		 */		
		public function getNPCWalkNode(clickPoint : Point):Point
		{
			if(isNPCWalk(clickPoint))
			{
				return clickPoint;
			}
			
			if( clickPoint.y < _startNPCPoint.y )
			{
				clickPoint.y = _startNPCPoint.y;
			}else if( clickPoint.y > _startNPCPoint.y + _NPCHiehgt )
			{
				clickPoint.y = _startNPCPoint.y + _NPCHiehgt;
			}
			
			if(clickPoint.x < _startNPCPoint.x)
			{
				clickPoint.x = _startNPCPoint.x;
			}
			else if(clickPoint.x > _startNPCPoint.x + _NPCWidth)
			{
				clickPoint.x = _startNPCPoint.x + _NPCWidth;
			}
			
			return clickPoint;
		}
	}
}